package com.chenfan.mcn.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 无重复且无NULL的ArrayList
 *
 * @author wulg
 * @date 2021-09-27
 **/
public class NoRepeatableArrayList<E> extends ArrayList<E> {

    public NoRepeatableArrayList(int initialCapacity) {
        super(initialCapacity);
    }

    public NoRepeatableArrayList() {
        super();
    }

    public NoRepeatableArrayList(Collection<? extends E> c) {
        super();
        this.addAll(c);
    }

    @Override
    public boolean add(E element) {
        if(element == null || super.contains(element)) {
            return false;
        }
        return super.add(element);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        if(c == null || c.size() == 0) {
            return false;
        }
        Iterator<? extends E> iterator = c.iterator();
        while(iterator.hasNext()) {
            this.add(iterator.next());
        }
        return true;
    }

    @Override
    public void add(int index, E element) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public E set(int index, E element) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
